home *** CD-ROM | disk | FTP | other *** search
/ Aminet 32 / Aminet 32 (1999)(Schatztruhe)[!][Aug 1999].iso / Aminet / dev / lang / Python151_Src.lha / Python1.5_Source / Amiga / _dup.c < prev    next >
C/C++ Source or Header  |  1998-12-25  |  3KB  |  118 lines

  1. RCS_ID_C="$Id: _dup.c,v 4.1 1994/09/29 23:09:02 jraja Exp $";
  2. /*
  3.  *      _dup.c - duplicate a file descriptor (SAS/C)
  4.  *
  5.  *      Copyright © 1994 AmiTCP/IP Group, 
  6.  *                       Network Solutions Development Inc.
  7.  *                       All rights reserved.
  8.  */
  9.  
  10. #include <ios1.h>
  11. #include <fcntl.h>
  12. #include <stdlib.h>
  13. #include <dos.h>
  14. #define USE_BUILTIN_MATH
  15. #include <string.h>
  16. #include <errno.h>
  17. #include <dos/dos.h>
  18. #include <proto/dos.h>
  19.  
  20. #include <bsdsocket.h>
  21. #include "libcheck.h"
  22.  
  23. /****** net.lib/dup ***********************************************************
  24.  
  25.     NAME
  26.         dup, dup2 - duplicate an existing file descriptor
  27.  
  28.     SYNOPSIS
  29.         #include <unistd.h>
  30.  
  31.         int dup(int oldd)
  32.  
  33.         int dup2(int oldd, int newd)
  34.  
  35.     FUNCTION
  36.         Dup() duplicates an existing object descriptor and returns its value
  37.         to the calling program (newd = dup(oldd)). The argument oldd is a
  38.         small nonnegative integer index in the program's descriptor table.
  39.         The value must be less than the size of the table, which is returned
  40.         by getdtablesize().  The new descriptor returned by the call is the
  41.         lowest numbered descriptor currently not in use by the program.
  42.  
  43.         The object referenced by the descriptor does not distinguish between
  44.         oldd and newd in any way.  Thus if newd and oldd are duplicate
  45.         references to an open file, read() and write() calls all move a single
  46.         pointer into the file, and append mode, non-blocking I/O and
  47.         asynchronous I/O options are shared between the references.  If a
  48.         separate pointer into the file is desired, a different object
  49.         reference to the file must be obtained by issuing an additional open()
  50.         call.  The close-on-exec flag on the new file descriptor is unset.
  51.  
  52.         In dup2(), the value of the new descriptor newd is specified.  If this
  53.         descriptor is already in use, the descriptor is first deallocated as
  54.         if a close() call had been done first.
  55.  
  56.     RETURN VALUES
  57.         The value -1 is returned if an error occurs in either call.  The
  58.         external variable errno indicates the cause of the error.
  59.  
  60.     BUGS
  61.         The current UFB implementation for SAS C allows only sockets to be
  62.         duplicated.
  63.  
  64.     ERRORS
  65.         Dup() and dup2() fail if:
  66.  
  67.         [EBADF]       Oldd or newd is not a valid active descriptor
  68.  
  69.         [EMFILE]      Too many descriptors are active.
  70.  
  71.     SEE ALSO
  72.         accept(),  open(),  close(),  socket(),  getdtablesize()
  73.  
  74.     STANDARDS
  75.         Dup() and dup2() are expected to conform to IEEE Std 1003.1-1988
  76.         (``POSIX'').
  77.  
  78.     COPYRIGHT
  79.         This manual page is copyright © 1980, 1991 Regents of the
  80.         University of California.  All rights reserved.
  81.  
  82. *******************************************************************************
  83. */
  84.  
  85.  
  86. int
  87. dup(int old_fd)
  88. {
  89.   struct UFB *ufb;
  90.   int ufbflg;
  91.   /*
  92.    * Check for the break signals
  93.    */
  94.   __chkabort();
  95.  
  96.   /*
  97.    * Find the ufb * for the given FD
  98.    */
  99.   if ((ufb = __chkufb(old_fd)) == NULL) {
  100.     errno = EBADF;
  101.     return -1;
  102.   }
  103.   
  104.   ufbflg = ufb->ufbflg;
  105.  
  106.   /* 
  107.    * The brain dead UFB system won't allow duplicating ordinary files
  108.    */
  109.   if ((ufbflg & UFB_SOCK) == UFB_SOCK) {
  110.     /* needs bsdsocket.library -- I.J. */
  111.     if(!checksocketlib()) return -1;
  112.     return Dup2Socket(old_fd, -1);
  113.   } else {
  114.     errno = EBADF;
  115.     return -1;
  116.   }
  117. }
  118.